package orchestration.android.demo.library;

import java.util.ArrayList;
import java.util.List;

import orchestration.android.OrchestratorApp;
import orchestration.android.R;
import orchestration.android.demo.library.LibraryProvider.Books;
import orchestration.android.demo.library.model.Library;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class LibraryServiceImpl extends Service {

	private static String TAG="makeLoanService";
	private IBinder binder= new ILibraryService.Stub(){

		@Override
		public List<Uri> getLoaned(String loaner) throws RemoteException {
			return getLoanedImpl(loaner);
		}

		@Override
		public List<Uri> getNotLoaned() throws RemoteException {
			return getNotLoanedImpl();
		}

		@Override
		public boolean loanBook(Uri bookUri, String loaner)
				throws RemoteException {
			boolean loanBookImpl = loanBookImpl(bookUri,loaner);
			return loanBookImpl;
		}

		@Override
		public boolean returnBook(Uri bookUri, String loaner)
				throws RemoteException {
			return returnBookImpl(bookUri,loaner);
		}

		@Override
		public boolean loanBookById(long id) throws RemoteException {
			Log.i(TAG, "loanBookById");
			return loanBookImpl(Uri.withAppendedPath(Books.CONTENT_URI, ""+id),"Carlo");
		}
		
	};
	@Override
		public int onStartCommand(Intent intent, int flags, int startId) {
			String loaner=intent.getStringExtra("loaner");
			long bookId= intent.getLongExtra(Library.KEY_ID, 0);
			Library.getInstance().makeLoan(this,bookId, loaner);
			Log.i(TAG, "service onStartCommand()");
			return super.onStartCommand(intent, flags, startId);
		}
	protected boolean returnBookImpl(Uri bookUri, String loaner) {
		long bookId = getBookIdFromUri(bookUri);
		Library.getInstance().returnBook(this, bookId, loaner);
		return true;
	}
	protected boolean loanBookImpl(Uri bookUri, String loaner) {
		long bookId= getBookIdFromUri(bookUri);
		Library.getInstance().makeLoan(this, bookId, loaner);
		NotificationManager notificationManager =
		    (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		Notification notification = new Notification(R.drawable.orchestrator,"Library",System.currentTimeMillis());
		notification.setLatestEventInfo(this, "Library service", "loading book with id: "+bookId, PendingIntent.getActivity(this, 0, null, 0));
		notificationManager.notify(((OrchestratorApp)getApplication()).newNotificationId(), notification);
		return true;
	}
	protected List<Uri> getNotLoanedImpl() {
		Cursor c=Library.getInstance().getNotLoanedBooks(this);
		List<Uri> result = createUriListFromCursor(c);
		return result;
	}
	private List<Uri> createUriListFromCursor(Cursor c) {
		c.moveToFirst();
		List<Uri> result= new ArrayList<Uri>();
		while(!c.isAfterLast()){
			String bookId=c.getString(c.getColumnIndex(Library.KEY_ID));
			result.add(Uri.withAppendedPath(Books.CONTENT_URI, bookId));
		}
		c.close();
		return result;
	}
	protected List<Uri> getLoanedImpl(String loaner) {
		Cursor c=Library.getInstance().getLoanedBooks(this,loaner);
		List<Uri> result = createUriListFromCursor(c);
		return result;
	}
	private long getBookIdFromUri(Uri bookUri) {
		long bookId=Long.parseLong(bookUri.getPathSegments().get(1));
		return bookId;
	}

	@Override
	public IBinder onBind(Intent intent) {
		return binder;
	}
	
	@Override
	public boolean onUnbind(Intent intent) {
		Log.w(TAG, "unbinding");
		return super.onUnbind(intent);
	}

}
